<?php

namespace App\Libs\Views;

class Exception extends View
{
    public function show($response) {
        $msg = '[exception: ' . get_class($response) . '] ';
        $msg .= '[code: ' . $response->getCode() . '] [' . $response->getMessage() . ']';
        \App\Libs\Log\Logger::error('runtime', $msg);

        $this->echoLine(get_class($response) . '(' . $response->getCode() . '): ' . $response->getMessage());
        $trace = array(
            'file' => $response->getFile(),
            'line' => $response->getLine(),
            'class' => get_class($response),
            'type' => '',
            'function' => '',
            'args' => array(),
        );
        $traces = $response->getTrace();
        array_unshift($traces, $trace);
        foreach ($traces as $i => $trace) {
            $this->echoLine($this->buildTrace($i, $trace));
        }
    }

    protected function buildTrace($i, $trace) {
        return sprintf('%d) %s(%d): %s%s%s(%s)',
            $i,
            isset($trace['file']) ? $trace['file'] : '',
            isset($trace['line']) ? $trace['line'] : -1,
            isset($trace['class']) ? $trace['class'] : '',
            isset($trace['type']) ? $trace['type'] : '',
            isset($trace['function']) ? $trace['function'] : '',
            isset($trace['args']) ? json_encode($trace['args']) : '[]'
        );
    }
    protected function echoLine($line, $eol = '</br>') {
        echo $line . $eol;
    }
}

